package JDK8New;

import enity.Person;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;

public class TestStreamApi {
    @Test
    public void test01(){
        ArrayList<Person> people=new ArrayList<>();
        people.add(new Person("李铭晗",22,"男",8500.0));
        people.add(new Person("梅花十三",18,"女",5800.0));
        people.add(new Person("伍六七",21,"男",6800.0));
        people.add(new Person("青凤",24,"男",6900.0));

        //过滤操作
        people.stream().filter(p->p.getSex().equals("女")).forEach(System.out::println);
        System.out.println("***************");
        //限制操作
        people.stream().limit(3).forEach(System.out::println);

    }

    //映射
    @Test
    public void test02(){
        List<String> a=Arrays.asList("aa","bb","cc");
        a.stream().map(str->str.toUpperCase(Locale.ROOT)).forEach(System.out::println);

        //获取名字长度等于3的人的名字
        ArrayList<Person> people=new ArrayList<>();
        people.add(new Person("李铭晗",22,"男",8500.0));
        people.add(new Person("梅花十三",18,"女",5800.0));
        people.add(new Person("伍六七",21,"男",6800.0));
        people.add(new Person("青凤",24,"男",6900.0));
        System.out.println("====================");
        people.stream().map(p->p.getName()).filter(c->c.length()==2).forEach(System.out::println);
    }

    //排序
    @Test
    public void test03(){
        ArrayList<Person> people=new ArrayList<>();
        people.add(new Person("李铭晗",22,"男",8500.0));
        people.add(new Person("梅花十三",18,"女",5800.0));
        people.add(new Person("伍六七",22,"男",6800.0));
        people.add(new Person("青凤",22,"男",8900.0));

        //先以年龄排序，年龄相同用工资
        people.stream().sorted(
                (p1,p2)->{
                   Integer ageValue= Integer.compare(p1.getAge(),p2.getAge());
                   if(ageValue!=0){
                       return ageValue;
                   }else {
                       return Double.compare(p1.getSalary(),p2.getSalary());
                   }
                }

        ).forEach(System.out::println);
    }

    @Test
    public void test04(){
        ArrayList<Person> people=Person.getPersons();
        //统计工资大于5000的有多少人
        Long a=people.stream().filter(p->p.getSalary()>8000).count();
        System.out.println(a);
        System.out.println("================");
        //统计工资最高人
        Optional<Person> p=people.stream().max((p1, p2)->Double.compare(p1.getSalary(),p2.getSalary()));
        System.out.println(p);
        System.out.println("======================");
        //工资最低人
        Optional<Person> p3=people.stream().min((p1, p2)->Double.compare(p1.getSalary(),p2.getSalary()));
        System.out.println(p3);
    }

    @Test
    public void test05(){
        //计算所有员工的工资总和
        ArrayList<Person> people=Person.getPersons();
        Optional<Double> o=people.stream().map(p->p.getSalary()).reduce((d1,d2)->d1+d2);
        System.out.println(o);
    }

    //收集
    @Test
    public void test06(){
        //收集工资大于6000元的人员
        ArrayList<Person> people=Person.getPersons();
        List<Person> p2= people.stream().filter(p1->p1.getSalary()>6000).collect(Collectors.toList());
        Set<Person> p3= people.stream().filter(p1->p1.getSalary()>6000).collect(Collectors.toSet());
        p2.forEach(System.out::println);


    }
    @Test
    public void Test07(){}

}
